# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
# from django.contrib.auth import authenticate, login
from .forms import UserForm, RegistrationForm, UserProfileForm, UserInfoForm
from django.contrib.auth.decorators import login_required
from .models import UserInfo, UserProfile
from django.contrib.auth.models import User


# Create your views here.
# def user_login(request):
#     if request.method == 'POST':
#         # 实例化登录的用户信息
#         login_form = LoginForm(request.POST)
#         print(login_form)
#         print(request.read())
#         if login_form.is_valid():
#             cd = login_form.cleaned_data
#             # 对用户的信息和数据库中保存的User信息进行验证
#             user = authenticate(username=cd['username'], password=cd['password'])
#
#             if user:
#                 # user验证通过，就进行登录操作
#                 login(request, user)
#                 return HttpResponse('Welcome You.You have been authenticated successfully')
#             else:
#                 return HttpResponse('Sorry. Your username or password is not right')
#         else:
#             return HttpResponse('Invalid login')
#
#     if request.method == 'GET':
#         # 如果是get请求，就返回登录的视图
#         login_form = LoginForm()
#         return render(request, 'account/login.html', {'form': login_form})


def register(request):
    if request.method == 'POST':
        user_form = RegistrationForm(request.POST)
        user_profile_form = UserProfileForm(request.POST)
        if user_form.is_valid()*user_profile_form.is_valid():
            new_user = user_form.save(commit=False)
            new_user.set_password(user_form.cleaned_data['password'])
            new_user.save()

            new_user_profile = user_profile_form.save(commit=False)
            new_user_profile.user = new_user
            new_user_profile.save()
            UserInfo.objects.create(user=new_user)
            return render(request, 'account/register_successful.html')
        else:
            return HttpResponse('Sorry, you can not register.')
    else:
        user_form = RegistrationForm()
        userprofile_form = UserProfileForm()
        return render(request, 'account/register.html', {'form': user_form, 'profile': userprofile_form})


@login_required(login_url='/account/login')
def myself(request):
    user = User.objects.get(username=request.user.username)
    user_profile = UserProfile.objects.get(user=user)
    user_info = UserInfo.objects.get(user=user)
    return render(request, 'account/myself.html', {'user': user, 'user_info': user_info, 'user_profile': user_profile})


@login_required(login_url='/account/login')
def myself_edit(request):
    user = User.objects.get(username=request.user.username)
    user_profile = UserProfile.objects.get(user=request.user)
    user_info = UserInfo.objects.get(user=request.user)

    if request.method == 'POST':
        user_form = UserForm(request.POST)
        user_profile_form = UserProfileForm(request.POST)
        user_info_form = UserInfoForm(request.POST)
        if user_form.is_valid() * user_profile_form.is_valid() * user_info_form.is_valid():
            user_cd = user_form.cleaned_data
            user_profile_cd = user_profile_form.cleaned_data
            user_info_cd = user_info_form.cleaned_data
            user.email = user_cd['email']
            user_profile.birth = user_profile_cd['birth']
            user_profile.phone = user_profile_cd['phone']
            user_info.school = user_info_cd['school']
            user_info.company = user_info_cd['company']
            user_info.profession = user_info_cd['profession']
            user_info.address = user_info_cd['address']
            user_info.about_me = user_info_cd['about_me']
            user.save()
            user_info.save()
            user_profile.save()
        return HttpResponseRedirect('/account/myself')
    else:
        user_form = UserForm(instance=request.user)
        user_profile_form = UserProfileForm(initial={'birth': user_profile.birth, 'phone': user_profile.phone})
        user_info_form = UserInfoForm(initial={
            'school': user_info.school,
            'company': user_info.company,
            'profession': user_info.profession,
            'address': user_info.address,
            'about_me': user_info.about_me}
        )
        return render(request, 'account/myself_edit.html', {'user_form': user_form,
                                                            'user_profile_form': user_profile_form,
                                                            'user_info_form': user_info_form})


def home(request):
    return render(request, 'base.html')
